package org.example.database.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.database.DTO.AdviceResumeDTO;
import org.example.database.DTO.OptimizedResumeDTO;
import org.example.database.entity.Advice;
import org.example.database.entity.RecruitmentPosition;
import org.example.database.entity.Resume;
import org.example.database.result.Result;
import org.example.database.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;


    /**
     * 上传并处理简历
     *
     * @param file 简历文件
     * @param studentNum
     * @return 处理结果
     */
    @PostMapping("/upload")
    public Result<?> uploadResume(@RequestParam("file") MultipartFile file ,Integer studentNum) throws IOException {
           try{
               Resume resume = resumeService.processResume(file,studentNum);
               Advice advice = resumeService.generateResumeAdvice(resume);
               return Result.success(advice);
           } catch (Exception e){
               return Result.error(e.getMessage());
           }
    }


    /**
     * 获取简历信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<?> getResume(@PathVariable Integer id){
        try {
            Resume resume = resumeService.getResumeById(id);
            return Result.success(resume);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取优化后的简历信息
     * @param resumeId 简历ID
     * @return 优化后的简历信息
     */
    @GetMapping("/optimize/{resumeId}")
    public Result<?> getOptimizedResume(@PathVariable Integer resumeId) {
        try {
            Resume resume = resumeService.getResumeById(resumeId);
            OptimizedResumeDTO optimizedResume = resumeService.generateOptimizedResume(resume);
            return Result.success(optimizedResume);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取推荐职位
     * @param resumeId
     * @return
     */
    @GetMapping("/recommendations/{resumeId}")
    public Result<?> getJobRecommendations(@PathVariable Integer resumeId){
        try{
            log.info("获取推荐职位");
            Resume resume = resumeService.getResumeById(resumeId);
            List<RecruitmentPosition> recommendations = resumeService.getRecommendedJobs(resume);
            return Result.success(recommendations);
        } catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


}
